#include <bits/stdc++.h>
#ifndef DEBUG
#define debug
#endif
using namespace std;
using i64 = int64_t;

int dp[9][9][9][9][16];

int main() {
  cin.tie(0)->sync_with_stdio(0);
  int n;
  cin >> n;
  vector<vector<int>> s(9, vector<int>(9));
  for (int i = 1; i <= 8; i ++) {
    for (int j = 1; j <= 8; j ++) {
      cin >> s[i][j];
      s[i][j] += s[i - 1][j] + s[i][j - 1] - s[i - 1][j - 1];
    }
  }
  auto calc = [&](int x1, int x2, int y1, int y2) {
    int v = s[x2][y2] - s[x1 - 1][y2] - s[x2][y1 - 1] + s[x1 - 1][y1 - 1];
    return v * v;
  };
  for (int l = 8; l >= 1; l --) {
    for (int r = l; r <= 8; r ++) {
      for (int d = 8; d >= 1; d --) {
        for (int u = d; u <= 8; u ++) {
          dp[l][r][d][u][1] = calc(l, r, d, u);
          for (int k = 2; k <= n; k ++) {
            auto &res = dp[l][r][d][u][k];
            res = INT_MAX / 2;
            for (int i = l; i < r; i ++) {
              res = min(res, dp[l][i][d][u][k - 1] + calc(i + 1, r, d, u));
              res = min(res, dp[i+1][r][d][u][k - 1] + calc(l, i, d, u));
            }
            for (int i = d; i < u; i ++) {
              res = min(res, dp[l][r][d][i][k - 1] + calc(l, r, i + 1, u));
              res = min(res, dp[l][r][i+1][u][k - 1] + calc(l, r, d, i));
            }
          }
        }
      }
    }
  }
  cout << dp[1][8][1][8][n] << '\n';
}